package org.dromara.system.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.BeanCopyUtils;
import org.dromara.system.mapper.SysNoticeMapper;
import org.dromara.system.pojo.bo.SysNoticeBO;
import org.dromara.system.pojo.entity.SysNoticeDO;
import org.dromara.system.pojo.qo.SysNoticeQO;
import org.dromara.system.pojo.vo.SysNoticeVO;
import org.dromara.system.service.SysNoticeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 通知公告表 服务层实现。
 *
 * @author mkj
 * @since 2024-07-21
 */
@RequiredArgsConstructor
@Service
public class SysNoticeServiceImpl implements SysNoticeService {

    private final SysNoticeMapper sysNoticeMapper;

    @Override
    public boolean add(SysNoticeBO sysNoticeBO) {
        SysNoticeDO sysNoticeDO = BeanCopyUtils.copy(sysNoticeBO, SysNoticeDO.class);
        return sysNoticeMapper.insert(sysNoticeDO) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return sysNoticeMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(SysNoticeBO sysNoticeBO) {
        SysNoticeDO sysNoticeDO = BeanCopyUtils.copy(sysNoticeBO, SysNoticeDO.class);
        return sysNoticeMapper.update(sysNoticeDO) > 0;
    }

    @Override
    public List<SysNoticeVO> list(SysNoticeQO sysNoticeQO) {
        SysNoticeDO sysNoticeDO = BeanCopyUtils.copy(sysNoticeQO, SysNoticeDO.class);
        QueryWrapper queryWrapper = QueryWrapper.create(sysNoticeDO);
        return sysNoticeMapper.selectListByQueryAs(queryWrapper, SysNoticeVO.class);
    }

    @Override
    public SysNoticeVO getInfo(Long id) {
        SysNoticeDO sysNoticeDO = sysNoticeMapper.selectOneById(id);
        if (sysNoticeDO == null) {
            return null;
        }
        return BeanCopyUtils.copy(sysNoticeDO, SysNoticeVO.class);
    }

    @Override
    public Page<SysNoticeVO> page(SysNoticeQO page) {
        return sysNoticeMapper.paginateAs(page.getPageNumber(), page.getPageSize(), QueryWrapper.create(page), SysNoticeVO.class);
    }

}

